import { BasePage, storage } from "../../common/index";
import SearchService from "../../services/search";
export default class ContentController extends BasePage {
  constructor(data) {
    super(data);
    this._registerPage(ContentController);
    this.searchService = new SearchService();
    this.storage = storage;
  }
  /**
   * 生命周期函数
   */
  onLoad(options) {
    this.data.chapter = this.storage.data.currentBookChapter;
    this.data.book = this.storage.data.currentBookDetail;
    this.data.pageIndex = this.data.book.currentChapter || 0;
    if (this.data.book.currentContent) {
      this.data.isSearch = false;
      this.data.content = this.data.book.currentContent;
    } else {
      this.searchContent();
    }
    wx.getSystemInfo({
      success: (res) => {
        this.data.windowIsBang =
          res.windowWidth / res.windowHeight < 0.5 ? true : false;
      },
    });
  }
  /**
   * 生命周期函数
   */
  onShow() {
    console.log("onShow--------------------->");
    console.log(this.storage.data.bookList);
  }
  hideChapter() {
    this.data.isShowChapter = false;
  }
  goBack() {
    console.log(this.storage.data.bookList);
    wx.navigateBack();
  }
  tabChange(e) {
    if (e.detail.index == 0) {
      // 上一章
      this.goToPrev();
      this.data.currentTab = -1;
    } else if (e.detail.index == 3) {
      // 下一章
      this.goToNext();
      this.data.currentTab = -1;
    } else if (e.detail.index == 1) {
      // 跳转到目录
      this.data.isShowChapter = true;
      this.data.isShowFooter = false;
      this.data.currentTab = -1;
      this.data.toView = "chapter" + this.data.pageIndex;
    } else if (e.detail.index == 2) {
      // if (this.data.isAdIndex) {
      //   wx.navigateTo({
      //     url: "../source/source?type=2",
      //   });
      // } else {
      //   wx.navigateTo({
      //     url: "../source/source",
      //   });
      // }
      wx.showToast({
        title: "暂未支持换源",
        icon: "success",
        duration: 1000,
      });
      this.data.currentTab = -1;
    }
  }
  goToContent(e) {
    this.data.pageIndex = e.currentTarget.dataset.index;
    this.data.chapter = this.data.book.chapter[this.data.pageIndex];
    this.data.isSearch = true;
    this.data.isShowChapter = false;
    this.searchContent();
  }
  goToPrev() {
    if (this.data.pageIndex == 0) {
      wx.showToast({
        title: "已经没有了",
        icon: "success",
        duration: 1000,
      });
      return;
    }
    let index = this.data.pageIndex - 1;
    this.data.chapter = this.data.book.chapter[index];
    this.data.isSearch = true;
    this.data.pageIndex = index;
    this.searchContent();
  }
  goToNext() {
    let index = this.data.pageIndex + 1;
    if (index > this.data.book.chapter.length - 1) {
      index = this.data.book.chapter.length - 1;
      wx.showToast({
        title: "已经没有了",
        icon: "success",
        duration: 1000,
      });
      return;
    }
    this.data.pageIndex = index;
    this.data.chapter = this.data.book.chapter[index];
    this.data.isSearch = true;
    this.searchContent();
  }
  /**
   * 展示工具栏
   */
  showFooter() {
    if (this.data.isloadtouch) {
      this.data.isShowFooter = false;
    } else {
      this.data.isShowFooter = true;
    }
  }
  /**
   * 因此工具栏
   */
  hideFooter() {
    this.data.isloadtouch = this.data.isShowFooter;
    if (this.data.isShowFooter) {
      this.data.isShowFooter = false;
    }
  }
  searchContent() {
    this.searchService
      .searchBook({
        id: this.data.book.sourceBookId,
        type: 3,
        href: this.data.chapter.href,
      })
      .then((data) => {
        data.content = data.content.replace(
          /\s+/g,
          '<br/><span style="display:inline-block;width:30px;"></span>'
        );
        this.data.content = data.content;
        this.data.isSearch = false;
        const bookList = this.storage.data.bookList || [];
        console.log(bookList);
        bookList.map((item) => {
          if (item.id === this.data.book.id) {
            item.currentChapter = this.data.pageIndex;
            item.currentContent = data.content;
          }
        });
        this.storage.data.bookList = bookList;
      });
  }
}

new ContentController({
  isSearch: true, // 是否查询内容 用来显示动画
  isloadtouch: false, // 是否滑动页面
  currentTab: -1, // 标记tab无选中装坛
  isShowFooter: false, // 是否显示tab页
  pageIndex: 0, // 当前阅读下标
  chapter: {},
  toView: "chapter0",
  book: {},
  isShowChapter: false,
  windowIsBang: false,
  content: "", // 书籍内容
  id: "", // 书籍id
  list: [
    {
      // tab页按钮
      text: "上一章",
      iconPath: "/assets/images/wenzhang.png",
      selectedIconPath: "/assets/images/wenzhang-select.png",
    },
    {
      text: "目录",
      iconPath: "/assets/images/mulu.png",
      selectedIconPath: "/assets/images/mulu-select.png",
    },
    {
      text: "换源",
      iconPath: "/assets/images/huanyuan.png",
      selectedIconPath: "/assets/images/huanyuan-select.png",
    },
    {
      text: "下一章",
      iconPath: "/assets/images/wenzhang.png",
      selectedIconPath: "/assets/images/wenzhang-select.png",
    },
  ],
});
